<?php
namespace common\extensions\pdf;

class Pdf extends PDF_Chinese
{
    public $title = '';
    public $widths;
    public $aligns;
    public $orientation;
    public $unit;
    public $size;

    public function __construct($orientation = 'P', $unit = 'mm', $size = 'A4')
    {
        $this->FPDF($orientation, $unit, $size);
    }

    function Header()
    {
        $this->SetFontSize(18);
        $this->Cell(0, 10, iconv('utf-8', 'gbk', $this->title), 0, 0, 'C');
        $this->Ln(18);
        $this->SetFontSize(12);
        $this->Cell(40, 10, iconv('utf-8', 'gbk', '学校名称：'), 0, 0, 'L');
        $this->Cell(40, 10, iconv('utf-8', 'gbk', '(盖章)'), 0, 0, 'R');
        $this->Ln(13);
        $this->Cell(40, 10, iconv('utf-8', 'gbk', '填表日期：'), 0, 0, 'L');
        $this->Cell(15, 10, iconv('utf-8', 'gbk', '年'), 0, 0, 'R');
        $this->Cell(15, 10, iconv('utf-8', 'gbk', '月'), 0, 0, 'R');
        $this->Cell(15, 10, iconv('utf-8', 'gbk', '日'), 0, 0, 'R');
        $this->Ln(13);
    }

//    function Footer() {
//        $this->SetFontSize(8);
//        $this->Cell(0,10,iconv('utf-8','gbk','备注：1、未批准招生的国控专业请附相关材料；'),0,0,'L');
//        $this->Ln(5);
//        $this->Cell(68,10,iconv('utf-8','gbk','2、此表请按照工作平台设计程序生成、打印。'),0,0,'R');
//    }

    function SetWidths($w)
    {
        //Set the array of column widths
        $this->widths = $w;
    }

    function SetAligns($a)
    {
        $this->aligns = $a;
    }

    function Row($data)
    {
        $this->SetFontSize(10);
        //Calculate the height of the row
        $nb = 0;
        $i = 0;
        foreach ($data as $k => $row) {
            $row = trim($row);
            $nb = max($nb, $this->NbLines($this->widths[$i], iconv('utf-8', 'gbk//TRANSLIT', $row)));
            ++$i;
        }
        $h = 5 * $nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        $i = 0;
        foreach ($data as $k => $row) {
            $row = trim($row);
            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            //Save the current position
            $x = $this->GetX();
            $y = $this->GetY();
            //Draw the border
            $this->Rect($x, $y, $w, $h);
            //Print the text
            $this->MultiCell($w, 5, iconv('utf-8', 'gbk//TRANSLIT', $row), 0, $a);
            //Put the position to the right of the cell
            $this->SetXY($x + $w, $y);
            ++$i;
        }
        //Go to the next line
        $this->Ln($h);
    }

    function CheckPageBreak($h)
    {
        //If the height h would cause an overflow, add a new page immediately
        if ($this->GetY() + $h > $this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);
    }

    function NbLines($w, $txt)
    {
        //Computes the number of lines a MultiCell of width w will take
        $cw =& $this->CurrentFont['cw'];
        if ($w == 0)
            $w = $this->w - $this->rMargin - $this->x;
        $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
        $s = str_replace("\r", '', $txt);

        $nb = strlen($s);
        if ($nb > 0 and $s[$nb - 1] == "\n")
            $nb--;
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while ($i < $nb) {
            $c = $s[$i];
            $ascii = (ord($c) < 128);
            if ($c == "\n") {
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
                continue;
            }
            if (!$ascii) {
                $sep = $i;
                $ls = $l;
            }
            if ($c == ' ') {
                $sep = $i;
                $ls = $l;
            }
            $l += $ascii ? $cw[$c] : 1000;
            if ($l > $wmax) {
                if ($sep == -1) {
                    if ($i == $j)
                        $i += $ascii ? 1 : 2;
                } else
                    $i = ($s[$sep] == ' ') ? $sep + 1 : $sep;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
            } else
                $i += $ascii ? 1 : 2;
        }
        return $nl;
    }
}

?>
